package edu.neusoft.springhtmldemo.mapper;

import edu.neusoft.springhtmldemo.domain.RobotManagement;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;

import java.util.List;

@Mapper
public interface RobotManagementMapper {

//    @Select("select dstation.datastationIP, robot.robotNum, broadcast.boradcastName,broadcast.broadcastIP,camera.cameraIP,dstation.datastationName\n" +
//            "from robotmanagemententity robot, robotdatastationrelationentiy robstation, datastationmanagemententity dstation,\n" +
//            "robotbroadcastrelationentity robbroadcast, broadcastmanagemententity broadcast,\n" +
//            "robotcamerarelationentiy robcamera, cameramanagemententity camera\n" +
//            "where robot.robotNum = robstation.robotNum\n" +
//            "and robstation.datastationID = dstation.datastationID\n" +
//            "and robot.robotNum = robbroadcast.robotNum\n" +
//            "and robbroadcast.broadcastID = broadcast.broadcastID\n" +
//            "and robcamera.robotNum = robot.robotNum\n" +
//            "and robcamera.cameraID = camera.cameraID")
    @Select("select * from robots.robotmanagement")
    List<RobotManagement> selectAll();

    @Select("select * from robots.robotmanagement \n" +
            "where robotNum like '%' #{robotNum} '%' \n" +
            "and datastationIP like '%' #{datastationIP} '%'\n" +
            "and broadcastIP like '%' #{broadcastIP} '%'\n" +
            "and cameraIP like '%' #{cameraIP} '%'\n" +
            "and boradcastName like '%' #{boradcastName} '%'\n" +
            "and datastationName like '%' #{datastationName} '%'")
    List<RobotManagement> selectRobotManagement(RobotManagement management);

    @Select("UPDATE robotdatastationrelationentiy \n" +
            "set datastationID=(select datastationID from datastationmanagemententity where datastationIP=#{datastationIP})\n" +
            "where robotNum=#{robotNum}")
    void updateRobotManagementDatastationIP(RobotManagement management);
    @Select("UPDATE robotcamerarelationentiy \n" +
            "set cameraID=(select cameraID from cameramanagemententity where cameraIP=#{cameraIP})\n" +
            "where robotNum=#{robotNum}")
    void updateRobotManagementcameraIP(RobotManagement management);
    @Select("UPDATE robotbroadcastrelationentity \n" +
            "set broadcastID=(select broadcastID from broadcastmanagemententity where broadcastIP=#{broadcastIP})\n" +
            "where robotNum=#{robotNum}")
    void updateRobotManagementbroadcastIP(RobotManagement management);

    @Select("select robotNum from robotmanagemententity")
    List<String> robotNumValues();

    @Select("insert into robotbroadcastrelationentity(robotNum, broadcastID) \n" +
            "VALUE(#{robotNum},(select broadcastID from broadcastmanagemententity where broadcastIP=#{broadcastIP}))")
    void addRobotBroadcastRelationEntity(RobotManagement management);
    @Select("insert into robotdatastationrelationentiy(robotNum, datastationID) \n" +
            "VALUE(#{robotNum},(SELECT datastationID from datastationmanagemententity where datastationIP=#{datastationIP}))")
    void addRobotDataStationRelationEntity(RobotManagement management);
    @Select("insert into robotcamerarelationentiy(robotNum, cameraID) \n" +
            "VALUE(#{robotNum},(SELECT cameraID from cameramanagemententity where cameraIP=#{cameraIP}))")
    void addRobotCameraRelationEntity(RobotManagement management);

    @Select("DELETE from robotbroadcastrelationentity\n" +
            "where robotNum=#{robotNum}\n" +
            "and broadcastID=(select broadcastID from broadcastmanagemententity where broadcastIP=#{broadcastIP})")
    void delRobotBroadcastRelationEntity(RobotManagement management);
    @Select("DELETE from robotdatastationrelationentiy\n" +
            "where robotNum=#{robotNum}\n" +
            "and datastationID=(SELECT datastationID from datastationmanagemententity where datastationIP=#{datastationIP})")
    void delRobotDataStationRelationEntity(RobotManagement management);
    @Select("DELETE from robotcamerarelationentiy\n" +
            "where robotNum=#{robotNum}\n" +
            "and cameraID=(SELECT cameraID from cameramanagemententity where cameraIP=#{cameraIP})")
    void delRobotCameraRelationEntity(RobotManagement management);
}
